package com.sznaner.pwinputview;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.constraintlayout.widget.ConstraintLayout;

public class PWInputDialog extends Dialog {

    private PWInputDialogListener listener;
    private PWInputDialogResetListener resetListener;
    public PWInputView inputView;
    private String title;

    private ConstraintLayout bgView;

    public PWInputDialog(Context context , String title,PWInputDialogListener listener) {
        super(context, R.style.dialog_style);//背景透明
        this.listener = listener;
        this.title = title;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(getLayoutInflater().inflate(R.layout.dialog_layout,null));

        inputView = findViewById(R.id.inputView);
        inputView.setTitle(title);
        bgView = findViewById(R.id.bgView);

        configWindow();

        bgView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hide();
            }
        });

        final PWInputDialog dialog = this;
        inputView.setOnFullListener(new PWInputView.OnFullListener() {
            @Override
            public void OnFull(String vaule) {
                if(listener != null){
                    listener.inputCompled(dialog,vaule);
                }
            }
        });
        inputView.setOnResetListener(new PWInputView.OnResetListener() {
            @Override
            public void OnReset() {
                if(resetListener != null) resetListener.onReset();
            }
        });
    }

    private void configWindow(){

        //设置Dialog全屏
        Window window = getWindow();
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(params);

        //        状态栏。只能有一个状态栏；它位于屏幕顶端，其他窗口都位于它下方。
//        public static final int TYPE_STATUS_BAR        =  FIRST_SYSTEM_WINDOW;
//        系统提示。它总是出现在应用程序窗口之上。
//        public static final int TYPE_SYSTEM_ALERT      =  FIRST_SYSTEM_WINDOW +3;
//        系统对话框。（例如音量调节框）。
//        public static final int TYPE_SYSTEM_DIALOG     =  FIRST_SYSTEM_WINDOW +8;
//        状态栏的滑动面板。
//        public static final int TYPE_STATUS_BAR_PANEL   = FIRST_SYSTEM_WINDOW +14;
//        系统窗口结束。
//        public static final int LAST_SYSTEM_WINDOW     = 2999;


        window.setGravity(Gravity.CENTER);
        //window.setWindowAnimations(R.style.PowerOffRebootAnim);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){//8.0+
            window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        }else {
            window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        }
        window.setDimAmount(0);//窗口透明
    }

//    /**
//     * 设置窗口位置
//     */
//
//    private void setWindow(){
//        Window window = getWindow();
//        if (window==null){
//            return;
//        }
//        WindowManager.LayoutParams params = window.getAttributes();
//        params.width = WindowManager.LayoutParams.MATCH_PARENT;
//        params.height = WindowManager.LayoutParams.MATCH_PARENT;
//        window.setAttributes(params);
//        window.setGravity(Gravity.CENTER);
//    }

    @Override
    public void show() {
        if (this.getWindow() != null) {
            this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
            super.show();
            this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        }else {
            super.show();
        }

        //setCanceledOnTouchOutside(true);//点击空白消失(背景全屏设置无效)
    }

    public void clearAll(){
        inputView.clearAll();
    }
    public void setShowPW(Boolean showPW) {
        inputView.setShowPW(showPW);
    }
    public void clearResetCount(){
        inputView.clearResetCount();
    }

    public void setResetListener(PWInputDialogResetListener resetListener) {
        this.resetListener = resetListener;
    }

    public interface PWInputDialogListener{
        void inputCompled(PWInputDialog dialog, String text);
    }

    public interface PWInputDialogResetListener{
        void onReset();
    }
}
